package com.exchange.context;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.utils.StringUtils;
import com.exchange.context.entity.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/***
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::' 
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 *
 * @Author liuhai
 * @Date 2020-11-25 10:46:57
 * @Description 过滤器
 */
@Component
@RefreshScope
public class TransmitUserInfoFilter implements Filter {

    @Value("${params.redis.user_token_key}")
    private String tokenKey;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final Logger log = LoggerFactory.getLogger(TransmitUserInfoFilter.class);
    public TransmitUserInfoFilter() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        this.initUserInfo((HttpServletRequest) servletRequest);
        filterChain.doFilter(servletRequest , servletResponse);
    }

    private void initUserInfo(HttpServletRequest request){
        String userTokenKey = request.getHeader(tokenKey);
        if (StringUtils.isNotBlank(userTokenKey)) {
            // 根据 key 从 redis 获取信息
            ValueOperations<String, String> ops = redisTemplate.opsForValue();
            String userStr = ops.get(userTokenKey);
            if(!org.springframework.util.StringUtils.isEmpty(userStr)){
                // 将 String 字符串转成 SysUser 对象
                UserInfo userInfo = JSON.parseObject(userStr, UserInfo.class);
                //将UserInfo放入上下文中
                UserInfoContext.setUser(userInfo);
                log.info("set UserInfoContext success");
            }
        }
    }

    @Override
    public void destroy() {

    }
}
